package asteroids.entity.equipment;

import java.awt.Color;
import java.awt.Graphics;
import java.util.ArrayList;

import asteroids.engine.GameState;
import asteroids.engine.Utils;
import asteroids.entity.GameObject;
import asteroids.entity.Ship;
import asteroids.factory.ShapeLib;

/**
 * Shield that automatically activates when the ship it's given is hit. Draws no
 * energy, but has limited uses (at least for now. May want to make it
 * probabilistically effective later on).
 * 
 * @author whitecd1
 * 
 */
public class AutoShield extends Equipment
{
    private final int SHIELDING_TIME = (int) (0.8 * Utils.FRAMES_PER_SECOND);
    private int shielding = 0;

    private int prior_x;
    private int prior_y;

    private int max_impacts;
    private int impacts_left;

    public AutoShield(Ship ship, int maximpacts)
    {
        super(ship);
        max_impacts = maximpacts;
        impacts_left = max_impacts;
        // max_radius = ship.getMaxRadius() * 3 / 2.0;
        max_radius = ShapeLib.shipLength;
        prior_x = (int) ship.getXPosition();
        prior_y = (int) ship.getYPosition();
        objectShape = new Utils.PolarCoordinate[8];
        for( int i = 0; i < 8; i++ )
        {
            objectShape[i] = Utils.createPolarCoordinate(i * (360 / 8), max_radius);
        }
    }

    public AutoShield(Ship ship)
    {
        this(ship, 3);
    }

    public void update(GameState state)
    {
        shielding = (shielding > 0) ? shielding - 1 : shielding;

        if( impacts_left > 0 || shielding > 0 )
        {
            ship.makeInvincible();
            // check for Bad Things!
            ArrayList<GameObject> possibleTargets = new ArrayList<GameObject>();
            possibleTargets.addAll(state.getAsteroidManager().getObjects()); // include
                                                                             // all
                                                                             // asteroids

            for( Ship s : state.getShipManager().getObjects() )
            {
                possibleTargets.add(s); // add all ships
                possibleTargets.addAll(s.getObjects()); // add all Bullets
            }
            for( GameObject other : possibleTargets )
            {
                if( Utils.gameObjectsCollide(ship, other) && other.isAlive()
                        && other.getAffiliation() != ship.getAffiliation()
                        && ship.getRespawnTimeLeft() == 0 )
                {
                    // only count a new impact if the previous
                    // "shielding" has worn off
                    if( shielding == 0 && impacts_left > 0 )
                    {
                        shielding = SHIELDING_TIME;
                        impacts_left--;
                    }
                }
            }
        }
    }

    public void respawn()
    {
        impacts_left = max_impacts;
    }

    public void draw(Graphics g)
    {
        if( shielding > 1 && ship.isAlive() ) // slight buffer so it erases
        {
            g.setColor(Color.WHITE);
            prior_x = (int) (ship.getXPosition() - max_radius);
            prior_y = (int) (ship.getYPosition() - max_radius);
            g.drawOval(prior_x, prior_y, (int) (max_radius * 2), (int) (max_radius * 2));
        }
    }

    public void erase(Graphics g)
    {
        if( shielding > 0 && ship.isAlive() )
        {
            g.setColor(Color.BLACK);
            g.drawOval(prior_x, prior_y, (int) (max_radius * 2), (int) (max_radius * 2));
        }
    }
}
